
# -*- coding: utf-8 -*-

'''
/*PHP是世界上最好的语言/*
'''

import requests,re,os
from base_config import *
from manage.exception import *

'''

1 如果没有 emptyCommentPlaceholder 被封号

2 如果有 没有 labelTextV2 标签为空

3 如果全有 正常


'''



class CommentTag( object ):

    def __init__(self):

        # 匹配评价标签
        self.pattern_text = re.compile(r'"text":"(.*?)"')
        # 匹配评价全部数量
        self.pattern_total = re.compile(r'"\u5168\u90e8\((.*?)\)"')
        #检测内容
        self.anti = 'labelTextV2'

    def getPage(self, params ):

        goodsid = params['goodsid']

        r = requests.get( url = domain_url['other']['pdd_tag_url'] + goodsid , headers=header)

        if self.anti not in r.text and 'emptyCommentPlaceholder' in r.text :

            print('[评价标签采集任务]: %s\t该商品暂无评价标签描述'% params['keywords'])

            # 任务采集成功接口上传
            suData = {
                'id': params['tasksid']
            }
            Exceptions.successTask(suData)

            return False

        elif 'emptyCommentPlaceholder' not in r.text:

            # 重启网卡突破反爬
            print('[评价标签采集任务]:IP被检测等待程序重新启动:%s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))))

            # 请求过快 异常返回任务失败
            data = {
                'id': params['tasksid']
            }

            Exceptions.failTask(data)

            os._exit(0)

        try:

            pattern = re.compile(r'window.rawData=(.*);')

            rawData = json.loads(re.search(pattern, r.text).group(1))

            items = (rawData['store']['tags'])

            for item in items:

                yield ('{0}({1})'.format(item['name'],item['num']))


        except:

            return False


    def parsePage(self,data):

        tags = self.getPage( data )

        if tags:

            for tag in tags:

                data['goodsid'] = data['goodsid']
                data['judge_lable'] = tag

                # 数据上传
                self.aipRequest( data )

            # 任务采集成功接口上传
            suData = {
                'id':data['tasksid']
            }
            Exceptions.successTask(suData)

    def aipRequest(self,data):

        r = requests.post( url = domain_url['domain']['test_tag_url'],data=data )

        if r.status_code == 200:

            print('[评价标签采集任务]: 评价信息上传成功:%s'%data['judge_lable'])

        return None

if __name__ == '__main__':


    t = CommentTag()

    data = {'id': '4', 'sortby': None, 'addby': None, 'createtime': '2019-08-21 16:52:48', 'editby': None, 'edittime': None, 'type': '3', 'keywords': None, 'classcode': None, 'goodsid': '36477933298', 'task_start_time': None, 'task_end_time': None, 'status': '0', 'recordscount': None, 'memo': '善存佳维片60片复合维生素b族b2钙片Vc补钙Ve成人补充维生素a', 'page': '0', 'processcode': '8C4DBBB9-EFB3-79A5-0F17-4F82DF440AD2', 'startTime': 1546272000, 'endTime': 1566316800}

    params = {

        'tasksid':data['id'],
        'goodsid':data['goodsid']
    }

    t.parsePage( params )



